import { GameManager, IUpdatable, regGameManager } from "../GameManager";
import { GameStorage, regStorage } from "../GameStorage";
import { getClientTimestampMS, getTimestampS, isNewDay } from "../common/Utils";
export class _Base_StorageBean {
    constructor(
        public lastCrossDayTimestamp: number = 0,
        public firstOpenGameTime = 0,
        public openGameTimes = 0,
        /**当日每个埋点发送的次数统计，跨天后清空 */
        public eventCountMap = {},
        public language = "zh_TW",

    ) { }

}

@regStorage(_Base_StorageBean, "_Base_Storage")
export class _Base_Storage extends GameStorage<_Base_StorageBean> {
    constructor(key?: string) {
        super(key)
        this._addOpenGameTimes()
    }
    crossDay(oldTimestamp: number, newTimestamp: number) {
        this.data.lastCrossDayTimestamp = newTimestamp
        this.data.eventCountMap = {}
        this.save();
    }
    async initLocation() {
    }

    get fistOpenDate() {
        if (!this.data.firstOpenGameTime) {
            this.data.firstOpenGameTime = Date.now()
        }
        return new Date(this.data.firstOpenGameTime)
    }
    /**增加游戏打开次数 */
    private _addOpenGameTimes() {
        this.data.openGameTimes++
        this.save()
    }

    get isFirstOpenGame() {
        return this.data.openGameTimes == 1
    }


}
/**本地数据管理器 */
@regGameManager()
export class LocalStorageManager extends GameManager implements IUpdatable {

    private _storageMap: Map<Object, GameStorage<any>> = new Map()
    protected _updates: IUpdatable[] = [];

    public async initialize(): Promise<void> {
        //注册到regStorage中的类型列表
        const clzArr: any[] = (regStorage as any).clzArr
        if (clzArr) {
            for (let index = 0; index < clzArr.length; index++) {
                const clz = clzArr[index]
                if (!this._storageMap.get(clz)) {
                    const storage: GameStorage<any> = new clz()
                    if ('update' in storage) {
                        this._updates.push(storage as unknown as IUpdatable)
                    }
                    this._storageMap.set(clz, storage)
                    storage.initialize && await storage.initialize()
                } else {
                    this._storageMap.get(clz).initialize && await this._storageMap.get(clz).initialize()
                }

            }
        }

    }

    public addStorage(clz: any) {
        console.log("addStorage", clz);

        const storage: GameStorage<any> = new clz()
        if ('update' in storage) {
            this._updates.push(storage as unknown as IUpdatable)
        }
        this._storageMap.set(clz, storage)

    }

    /**获取一个存储仓库 */
    public storage<T extends GameStorage<any>>(storageClz: (new () => T)) {
        if (!this._storageMap.has(storageClz)) {
            this.addStorage(storageClz)
        }
        return this._storageMap.get(storageClz) as T
    }

    update(dt: number): void {
        this._updates.forEach((i) => {
            i.update(dt)
        })
        this.checkCrossDay()
    }

    /**检查是否跨天 */
    checkCrossDay() {

        const bs = this.storage(_Base_Storage)
        const currentTimestampMs = getClientTimestampMS();
        const isCrossDay = isNewDay(bs.data.lastCrossDayTimestamp, currentTimestampMs)
        if (isCrossDay) {
            bs.crossDay(bs.data.lastCrossDayTimestamp, currentTimestampMs)
            this._storageMap.forEach((storage) => {
                storage.onCrossDay()
            })
        }

    }
}